package com.yyyyglxt.yyyh.controller;

import com.yyyyglxt.common.controller.BaseController;
import com.yyyyglxt.system.domain.DeptDO;
import com.yyyyglxt.system.service.DeptService;
import com.yyyyglxt.system.service.UserService;
import com.yyyyglxt.yygl.recordManage.domain.RecordDO;
import com.yyyyglxt.yygl.recordManage.service.RecordService;
import com.yyyyglxt.yygl.workdayManage.domain.WorkdayDO;
import com.yyyyglxt.yygl.workdayManage.service.WorkdayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author qichao
 * @date 2022/4/24 19:54
 *
 * 用户界面Controller
 *
 */

@Controller
@RequestMapping("/yyyh")
public class YyyhController extends BaseController {

    @Autowired
    private WorkdayService workdayService;

    @Autowired
    private RecordService recordService;

    @Autowired
    DeptService deptService;

    @Autowired
    UserService userService;

    @GetMapping()
    String index() {
        return "yyyh/index";
    }

    @GetMapping("/doctors")
    String doctors(@RequestParam Map<String, Object> params, Model model) {
        if(params.get("deptId")!=null){
            Long deptId = Long.valueOf(params.get("deptId").toString());
            List<Long> deptIds = deptService.listChildrenIds(deptId);
            model.addAttribute("deptIds",deptIds);
        }
        return "yyyh/doctors";
    }

    @GetMapping("/doctorDetail")
    String doctorDetail(@RequestParam Map<String, Object> params, Model model){
        liveListByDoc(params,model);
        model.addAttribute("docName",userService.get(Long.valueOf(params.get("did").toString())).getName());
        return "yyyh/doctorDetail";
    }

    //根据医生将实时排班信息列表封装到model
    public void liveListByDoc(@RequestParam Map<String, Object> params, Model model){
        /*** 获取医生的固定排班 ***/
        List<WorkdayDO> workdayList = workdayService.list(params);
        //获取接下来7天的日期
        String[] nextWeekDate = new String[7];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int weekday = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if(weekday <= 0) weekday = 7;
        weekday-=1;//星期对应下标  比如周六 是 5
        model.addAttribute("weekday",weekday+1);
        for (int i=0;i<7;i++){
            nextWeekDate[weekday] = sdf.format(cal.getTime());
            cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 1);
            weekday++;
            weekday%=7;
        }
        model.addAttribute("nextWeekDate",nextWeekDate);
        /*** 根据预约情况，计算实时号源信息 ***/
        List<RecordDO> recordList = recordService.list(params);
        //获取上午的实时排班信息
        List<WorkdayDO> amLiveWorkdayList = workdayList.stream().filter(item ->  "上午".equals(item.getAmpm())).sorted(Comparator.comparing(WorkdayDO::getWorktime)).collect(Collectors.toList());
        if(amLiveWorkdayList.size()==0) return;//管理员没有排班信息
        for (int i = 0; i < nextWeekDate.length; i++) {
            String dateStr = nextWeekDate[i];
            int amRecordCount = recordList.stream().filter(item -> sdf.format(item.getVisitdate()).equals(dateStr) && item.getVisitnoon().equals("上午")&&item.getState().equals("成功")).collect(Collectors.toList()).size();
            amLiveWorkdayList.get(i).setNsnum(amLiveWorkdayList.get(i).getNsnum()-amRecordCount);
        }
        model.addAttribute("amLiveWorkdayList",amLiveWorkdayList);
        //获取上午的实时排班信息
        List<WorkdayDO> pmLiveWorkdayList = workdayList.stream().filter(item ->  "下午".equals(item.getAmpm())).sorted(Comparator.comparing(WorkdayDO::getWorktime)).collect(Collectors.toList());
        for (int i = 0; i < nextWeekDate.length; i++) {
            String dateStr = nextWeekDate[i];
            int pmRecordCount = recordList.stream().filter(item -> sdf.format(item.getVisitdate()).equals(dateStr) && item.getVisitnoon().equals("下午")&&item.getState().equals("成功")).collect(Collectors.toList()).size();
            pmLiveWorkdayList.get(i).setNsnum(pmLiveWorkdayList.get(i).getNsnum()-pmRecordCount);
        }
        model.addAttribute("pmLiveWorkdayList",pmLiveWorkdayList);
    }

    @GetMapping("/myRecord")
    String myRecord(Model model) {
        model.addAttribute("userId",getUserId());
        return "yyyh/myRecord";
    }

    @GetMapping("/depts")
    String depts(Model model) {
        Map<String, Object> params = new HashMap<>();
        params.put("parentId","0");
        List<DeptDO> list = deptService.list(params);
        model.addAttribute("deptList",list);
        return "yyyh/depts";
    }

    @GetMapping("/register")
    String register(Model model) {
        return "yyyh/register";
    }

}
